<template>
    <el-main>
        <el-card v-if="announDialog.visible" :body-style="{ height:'80vh'}" class="card1">
            <div class="centent">
                <el-form :model="announcementInfo" ref="announcementForm" :rules="rules" label-width="100px" >
                <el-form-item label="标题:" prop="title">
                    <el-input v-model="announcementInfo.title" class="input"></el-input>
                </el-form-item>
                <el-form-item label="公告类型：" prop="type">
                <el-select v-model="announcementInfo.type" clearable placeholder="选择公告类型">
                            <el-option
                            v-for="item in typeProps"
                            :key="item.id"
                            :label="item.ladel"
                            :value="item.id"
                            >
                            </el-option>
                        </el-select>
            </el-form-item>
            <el-form-item label="内容：" prop="content">
                <el-input
                    type="textarea"
                    :rows="11"
                    placeholder="请输入内容"
                    v-model="announcementInfo.content"
                    style="width: 600px;">
                    </el-input>
            </el-form-item>
            <el-form-item label="备注：" prop="remark">
                <el-input
                    type="textarea"
                    :rows="3"
                    placeholder="请输入内容"
                    v-model="announcementInfo.remark"
                    style="width: 600px;">
                    </el-input>
            </el-form-item>
            </el-form>
                <div class="button">
                    <el-button type="success" @click="onConfirm()">确定</el-button>
                    <el-button @click="onClose()">取消</el-button>
                </div>
            </div>
        </el-card>
        <el-card v-if="!announDialog.visible">
            <!-- 查询条件 -->
        <el-form :model="searchModel" ref="searchForm" label-width="80px" :inline="true" size="small">
            <el-form-item label="标题：">
                <el-input v-model="searchModel.title" placeholder="请输入标题"></el-input>
            </el-form-item>
            <el-form-item label="公告类型">
                <el-select v-model="searchModel.type" clearable placeholder="默认所有类型">
                            <el-option
                            v-for="item in typeProps"
                            :key="item.id"
                            :label="item.ladel"
                            :value="item.id"
                            >
                            </el-option>
                        </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="search(pageNo,pageSize)">查询</el-button>
                <el-button icon="el-icon-refresh-right" @click="resetValue()">重置</el-button>
                <el-button type="success" icon="el-icon-plus" @click="openAddWindow()"
                v-if="hasPermission('sys:announcement:add')">新增</el-button>
            </el-form-item>
        </el-form>
        </el-card>
        <p></p>
        <el-card v-if="!announDialog.visible">
            <el-table
                :data="announcementList"
                stripe
                style="width: 100%">

            <el-table-column
             type="index"
             width="50"></el-table-column>

             <template v-for="item in tableProp">
                <el-table-column
                 v-if="item.prop !== 'type'"
                 :prop="item.prop" :label="item.label"
                 :formatter="item.formatter" :width="item.width">
                </el-table-column>

                <el-table-column
                    v-if="item.prop ==='type'"
                    :key="item.prop"
                    :prop="item.prop"
                    :label="item.label"
                >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type == 1" effect="dark">
                通知
              </el-tag>
              <el-tag v-if="scope.row.type == 2" effect="dark" type='warning'>
                公告
              </el-tag>
          </template>
        </el-table-column>

            </template>
            <el-table-column align="center" width="290" label="操作">
                <template slot-scope="scope">
                    <el-button icon="el-icon-edit" type="primary" size="mini" @click="updateAnnouncement(scope.row)"
                    v-if="hasPermission('sys:announcement:edit')">编辑</el-button>
                    <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteAnnouncement(scope.row)"
                    v-if="hasPermission('sys:announcement:delete')">删除</el-button>
                    <el-button type="info" size="mini" @click="check(scope.row)">详细</el-button>
                </template>
            </el-table-column>
            </el-table>
            <el-drawer
                :visible.sync="content.drawer"
                :direction="content.direction"
                :with-header="false"
                size="50%">
                <div class="tk"> 
                    <div class="header"><h3>{{ this.announcement.title }}</h3></div>
                    <div class="zxwz">
                        <p>发布于：{{ this.announcement.createTime }}</p>
                        <tr></tr>
                        <p class="indent">{{ this.announcement.content }}</p>
                    </div>
                    <div class="foot">
                        创建人：{{ this.announcement.createUser }}
                    </div>
                </div>
            </el-drawer>

            <!-- 分页工具栏 -->
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="pageNo" :page-sizes="[10, 20, 30, 40, 50]" :page-size="10"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </el-card>
    </el-main>
</template>

<script>
import announcementApi from '@/api/announcement';
import userApi from '@/api/user';
import SystemDialog from '@/components/system/SystemDialog.vue'
import { async } from 'q';
import dialog from './dialog.vue';

export default{
    name:'announcement',
    components:{ SystemDialog },
    data() {
        return {
            searchModel:{
                title:'',
                type:null,
                pageNo:1,
                pageSize:10,
            },
            userList:[],
            announcementList:[],
            announDialog:{
                title:'',
                visible:false,
            },
            announcementInfo:{
                id:null,
                title:'',
                type:null,
                content:null,
                createUserId:null,
                remark:null
            },
            announcement:{},
            typeProps:[
                { id:1,ladel:'通知' },
                { id:2,ladel:'公告' }
            ],
            tableProp:[
                { prop:'title', label:'公告标题', width:'250px'},
                { prop:'type', label:'公告类型', width:'30px'},
                { prop:'createUserId', label:'创建人',
                formatter:value => {
                        const cuName = this.userList.find(user => user.id === value.createUserId)
                        return cuName !== undefined ? cuName.nickName : '-'
                    }},
                { prop:'createTime', label:'创建时间'},
                { prop:'updateTime', label:'修改时间'},
            ],
            rules:{
                title: [{ required: true, trigger: "blur", message: "标题不能为空" }],
                type: [{ required: true, trigger: "change", message: "请选择公告类型" }],
                content: [{ required: true, trigger: "blur", message: "内容不能为空" }],
            },
            content:{
                drawer: false,
                direction:'rtl'
            },
            pageNo:1,
            pageSize:10,
            total:0,
        }
    },
    methods: {
        async search(pageNo,pageSize){
            this.searchModel.pageNo = pageNo
            this.searchModel.pageSize = pageSize
            let res = await announcementApi.getAnnouncementList(this.searchModel)
            if(res.success){
                this.announcementList = res.data.records
                this.total = res.data.total
            }
        },
        async getUserList(){
            let res = await userApi.getUserAll()
            if(res.success){
                this.userList = res.data
            }
        },
        resetValue(){
            this.searchModel = {}
            this.search()
        },
        openAddWindow(){
            this.$resetForm('announcementForm',this.announcementInfo)
            this.announDialog.title = '添加公告'
            this.announDialog.visible = true
        },
        onConfirm(){
            this.announcementInfo.createUserId = this.$store.getters.userId
            this.$refs.announcementForm.validate(async (valid) => {
                if(valid){
                    let res = null
                    if(this.announDialog.title == '添加公告'){
                        res = await announcementApi.addAnnouncement(this.announcementInfo)
                    }else if(this.announDialog.title == '修改公告'){
                        res = await announcementApi.updeteAnnouncementById(this.announcementInfo)
                    }
                    if(res.success){
                        this.$message.success(res.message)
                        this.search()
                        this.announDialog.visible = false
                    }
                }
            })
        },
        onClose(){
            this.announDialog.visible = false
        },
        updateAnnouncement(row){
            this.announDialog.title = '修改公告'
            this.announDialog.visible = true
            this.$objCopy(row,this.announcementInfo)
        },
        async deleteAnnouncement(row){
            let confirm = await this.$myconfirm("确定删除吗？")
            if(confirm){
                let res = await announcementApi.deleteAnnouncementById({id:row.id})
                if(res.success){
                    this.search()
                    this.$message.success(res.message)
                }else{
                    this.$message.error(res.message)
                }
            }
        },
        async check(row){
            let res = await announcementApi.getAnnouncementList({id:row.id})
            if(res.success){
                this.announcement = res.data.records[0]
                let res2 = await userApi.getUserList({id:row.createUserId})
                this.announcement.createUser = res2.data.records[0].nickName
                this.content.drawer = true
            }
        },
        // 页数发生变化
        handleSizeChange(size) {
            this.pageSize = size
            this.search(this.pageNo, size)
        },
        // 页码发生变化
        handleCurrentChange(page) {
            console.log(page)
            this.pageNo = page
            this.search(page, this.pageSize)
        },
    },
    created() {
        this.search()
        this.getUserList()
    },
}

</script>

<style lang="scss" scoped>
.card1{
    background: url('../../../../public/images/bg1.jpg');
}
.input{
    width: 200px;
}
.button{
    width: 180px;
    margin-top: 30px;
    float: right;
}
.centent{
    width: 800px;
    margin: auto;
}
.tk{

}
.zxwz{
    padding: 30px;
}
.indent{
    text-indent: 2em;
    font-size: 16px;
    line-height: 29px;
}
.header{
    text-align: center;
}
.foot{
    padding: 30px;
    font-size: 17px;
    text-align: right;
}
</style>